<?php

class MThesis extends CI_Model{

	public function __construct()
	    {
	        parent::__construct();
	    }
	
	
	function getThesis($id){
	    $data = array();
	    $this->db->where('thesis_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('thesis');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;    
	 }
	 function getLecturerList() {
	 	 $data = array();
	     $this->db->select('uid,firstname,lastname');
	      $this->db->order_by("lastname", "ASC");
	     $Q = $this->db->get('lecturer');
	     if ($Q->num_rows() > 0){
	       $data[0] = '...select adviser...';
	       foreach ($Q->result_array() as $row){
	         $data[$row['uid']] = $row['lastname'].' '.$row['firstname'];
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }	
	 
	 function getAllThesis(){
	     $data = array();
	     $this->db->select('thesis.*, lecturer.firstname, lecturer.lastname');
	     $this->db->from('thesis');
	     $this->db->join('lecturer', 'thesis.uid = lecturer.uid');
	     $this->db->order_by("topic", "ASC");
	     $Q = $this->db->get();
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
	
	function search(){
	     $data = array();
	     $uid  	= $_POST['uid'];
	     $topic = $_POST['topic'];
	     $fname = $_POST['firstname'];
	     $lname = $_POST['lastname'];
	     
	     $sql  = "SELECT thesis.*, lecturer.firstname, lecturer.lastname FROM thesis \n";
	     $sql .= "INNER JOIN lecturer ON thesis.uid=lecturer.uid WHERE \n";
	     $sql .= "thesis.topic LIKE '%".$topic."%' \n";
	     if ($uid > 0) {
	     	$sql .= "AND thesis.uid =".$uid." \n";
	     }
	     if (strlen($fname) > 0){
	     	$sql .= "AND (thesis.stud1_fname LIKE '%".$fname."%' OR thesis.stud2_fname LIKE '%".$fname."%' OR thesis.stud3_fname LIKE '%".$fname."%' OR thesis.stud4_fname LIKE '%".$fname."%') \n";
	     }
	     if (strlen($lname) > 0){
	     	$sql .= "AND (thesis.stud1_lname LIKE '%".$lname."%' OR thesis.stud2_lname LIKE '%".$lname."%' OR thesis.stud3_lname LIKE '%".$lname."%' OR thesis.stud4_lname LIKE '%".$lname."%') \n";
	     }
	     $sql .= "ORDER BY topic";
	     $Q = $this->db->query($sql); 
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[] = $row;
	       }
	    }
	    $Q->free_result();  
	    return $data; 
	 }
		
	 function addThesis(){
		$data = array( 
			'topic' => db_clean($_POST['topic']),
			'stud1_fname' => db_clean($_POST['stud1_fname']),
			'stud1_lname' => db_clean($_POST['stud1_lname']),
			'stud1_id' => $_POST['stud1_id'],
			'stud1_group' => $_POST['stud1_group'],
			'stud1_phone' => $_POST['stud1_phone'],
			'stud2_fname' => db_clean($_POST['stud2_fname']),
			'stud2_lname' => db_clean($_POST['stud2_lname']),
			'stud2_id' => $_POST['stud2_id'],
			'stud2_group' => $_POST['stud2_group'],
			'stud2_phone' => $_POST['stud2_phone'],
			'stud3_fname' => db_clean($_POST['stud3_fname']),
			'stud3_lname' => db_clean($_POST['stud3_lname']),
			'stud3_id' => $_POST['stud3_id'],
			'stud3_group' => $_POST['stud3_group'],
			'stud3_phone' => $_POST['stud3_phone'],
			'stud4_fname' => db_clean($_POST['stud4_fname']),
			'stud4_lname' => db_clean($_POST['stud4_lname']),
			'stud4_id' => $_POST['stud4_id'],
			'stud4_group' => $_POST['stud4_group'],
			'stud4_phone' => $_POST['stud4_phone'],
			'uid' => db_clean($_POST['uid']),
			'batch' => db_clean($_POST['batch']),
			'status' => db_clean($_POST['status']),
			'activate' => db_clean($_POST['activate'])
		);
		//$this->db->set('date_created', 'NOW()', FALSE);
		$this->db->insert('thesis', $data);	 
	 }
	 
	 function updateThesis(){
		$data = array( 
			'topic' => db_clean($_POST['topic']),
			'stud1_fname' => db_clean($_POST['stud1_fname']),
			'stud1_lname' => db_clean($_POST['stud1_lname']),
			'stud1_id' => $_POST['stud1_id'],
			'stud1_group' => $_POST['stud1_group'],
			'stud1_phone' => $_POST['stud1_phone'],
			'stud2_fname' => db_clean($_POST['stud2_fname']),
			'stud2_lname' => db_clean($_POST['stud2_lname']),
			'stud2_id' => $_POST['stud2_id'],
			'stud2_group' => $_POST['stud2_group'],
			'stud2_phone' => $_POST['stud2_phone'],
			'stud3_fname' => db_clean($_POST['stud3_fname']),
			'stud3_lname' => db_clean($_POST['stud3_lname']),
			'stud3_id' => $_POST['stud3_id'],
			'stud3_group' => $_POST['stud3_group'],
			'stud3_phone' => $_POST['stud3_phone'],
			'stud4_fname' => db_clean($_POST['stud4_fname']),
			'stud4_lname' => db_clean($_POST['stud4_lname']),
			'stud4_id' => $_POST['stud4_id'],
			'stud4_group' => $_POST['stud4_group'],
			'stud4_phone' => $_POST['stud4_phone'],
			'uid' => db_clean($_POST['uid']),
			'batch' => db_clean($_POST['batch']),
			'status' => db_clean($_POST['status']),
			'activate' => db_clean($_POST['activate'])
		);
		$this->db->where('thesis_id', id_clean($_POST['thesis_id']));
		$this->db->update('thesis', $data);	
	 
	 }
	 function inactiveThesis($id){
	 	$data = array('activate' => 0);
	 	$this->db->where('thesis_id', id_clean($id));
		$this->db->update('thesis', $data);	
	 }
	 
	 function deleteThesis($id){
	 	$this->db->where('thesis_id', id_clean($id));
		$this->db->delete('thesis');	
	 }
	 
	
}

?>